package com.example.drive.admin.controller;


import com.example.drive.admin.pojo.dto.UpdatePwdDTO;
import com.example.drive.admin.pojo.dto.StudentDTO;
import com.example.drive.admin.pojo.dto.UserDTO;
import com.example.drive.admin.pojo.entity.Detail;
import com.example.drive.admin.pojo.vo.UserLoginVO;
import com.example.drive.admin.service.AdminService;
import com.example.drive.common.JWTUtils;
import com.example.drive.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 系统管理员
 */

@RestController
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 用户登录
     * @param
     * @return
     */
    @PostMapping("login")
    public R<UserLoginVO> Login(@RequestBody @Validated UserDTO userDTO){
        UserLoginVO userLoginVO=adminService.login(userDTO);
        return R.ok(userLoginVO);
    }

    /**
     * 注册学员
     * @param student
     * @return
     */
    @PostMapping("register")
    public R<Void> Register(@RequestBody @Validated StudentDTO student){
        System.out.println(student.getUsername());
        adminService.register(student);
        return R.ok();
    }

    /**
     * 修改密码
     * @param updatePwdDTO
     * @return
     */
    @PutMapping("change")
    public R<Void> ChangePassword(@RequestBody @Validated UpdatePwdDTO updatePwdDTO, HttpServletRequest request){
        // 获取用户的身份：请求头中是含有token的
        String token = request.getHeader("Authorization");
        // 解析token：知道用户的身份
        UserLoginVO loinUser = JWTUtils.parseToken(token);
        adminService.change(updatePwdDTO,loinUser);
        return R.ok();
    }

    /**
     * 获取总业绩数据
     * @return
     */
    @GetMapping("detail/{id}")
    public R<Detail> getDetail(@PathVariable Integer id){
        Detail detail = adminService.getDetail(id);
        return R.ok(detail);
   }
}
